from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from api.api_view import Th_Create_Review
from api.models import IDRS
from common.pyside2_table.qt_wgt import show_msg
from common.tb_model import update_many_model

from api.api_project import *
from view.table_col_list import idrs_col_model_list
from view.tab_widget import Review_Table


class View_IDRS(QWidget):
    def __init__(self):
        super().__init__()
        self.view_name = "tab_drcs"
        self.data = public_data
        self.setupUi()
        self.bind()
        self.init_cmb()

    def setupUi(self):
        # region 布局
        # 整体布局为垂直布局
        lt_pg = QVBoxLayout(self)
        fm_ctrl = QFrame()

        lt_pg.addWidget(fm_ctrl)
        lt_header = QHBoxLayout(fm_ctrl)

        # endregion

        # region 项目、制造厂及采购包选择

        # 左侧项目cmb
        left_spacer = QSpacerItem(120, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)
        self.lb_project = QLabel("项目")
        self.cmb_project = QComboBox()
        self.lb_lot = QLabel("采购包")
        self.cmb_lot_code = QComboBox()
        self.lb_factory = QLabel("制造厂")
        self.cmb_factory = QComboBox()
        self.ck_has_delivery = QCheckBox(text="隐藏完工合同")

        wgt_list_left = [self.lb_project, self.cmb_project, self.lb_lot, self.cmb_lot_code, self.lb_factory,
                         self.cmb_factory, self.ck_has_delivery]
        for item in wgt_list_left:
            lt_header.addWidget(item)
        lt_header.addItem(left_spacer)

        # endregion

        # region 操作DRCS和IDRS
        # right_spacer = QSpacerItem(50, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.btn_delete = QPushButton("删除")
        self.btn_create = QPushButton("保存并发起流程")
        self.btn_reprush = QPushButton("刷新信息")
        lt_header.addWidget(self.btn_delete)
        lt_header.addWidget(self.btn_create)
        lt_header.addWidget(self.btn_reprush)
        # endregion

        # region 信息展示标签和表格
        self.lb_message = QLabel(text="已处理了XX个DRCS，待处理的DRCS数量为")
        self.lb_message.setAutoFillBackground(False)
        self.lb_message.setWordWrap(True)
        lt_pg.addWidget(self.lb_message, 0, Qt.AlignLeft)

        self.table = Review_Table("idrs", idrs_col_model_list, IDRS, )
        lt_pg.addWidget(self.table)

        # endregion

    def bind(self):
        self.cmb_project.currentTextChanged.connect(self.cmb_change)
        self.cmb_lot_code.currentTextChanged.connect(self.cmb_change)
        self.cmb_factory.currentTextChanged.connect(self.cmb_change)
        # self.ck_only_newest.clicked.connect(self.cmb_change)
        self.btn_create.clicked.connect(self.create_page)
        self.btn_reprush.clicked.connect(self.table.reset)

    def update_doc_view(self, condition):
        show_msg(f"已完成查询，共查新{2}条", "blue", self.lb_message)

    def init_cmb(self):
        self.cmb_project.addItems(self.data.Project_list)
        self.cmb_factory.addItems(self.data.Factory_list)
        self.cmb_lot_code.addItems(self.data.Lot_list)

    def cmb_change(self):
        lot_code = self.cmb_lot_code.currentText()
        factory = self.cmb_factory.currentText()
        project = self.cmb_project.currentText()
        change_dic = {"lot_code": lot_code, "factory": factory, "project": project}
        print("选项卡变化了", [lot_code, factory, project])

    def create_page(self):
        model_list = self.table.datas
        if model_list:
            no_create_model = []
            for model in model_list:
                if model.status == 0:
                    no_create_model.append(model)
            if no_create_model:
                th_drcs = Th_Create_Review(no_create_model, review_type="idrs")
                th_drcs.signal_send.connect(self.save_models)

    def save_models(self, result):
        update_many_model(self.table.tb_name, self.table.datas)
        if result:
            msg = f'已完成{len(self.idrs_list)}个DRCS的创建'
        else:
            msg = 'IDRS的创建失败'
        QMessageBox.information(self, "创建IDRS结果", msg, QMessageBox.Ok)
